//
//  CommentViewController.m
//  Dear
//
//  Created by n22 on 15/8/18.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "CommentViewController.h"
//#import <BaiduMapAPI/BMapKit.h>
//#import "LocationTool.h"
#import "UploadRequest.h"
@interface CommentViewController ()<UITextViewDelegate,LocationGetAddressDelegate>
{
  
}
@end

@implementation CommentViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

            }
    return self;
}
- (void)bulidLayout{
    [self.placeTextView initObserver];
    self.placeTextView.layer.masksToBounds = YES;
    self.placeTextView.layer.cornerRadius = 5;
    self.placeTextView.delegate = self;
    self.placeTextView.returnKeyType = UIReturnKeyDone;
    self.placeTextView.font = [UIFont systemFontOfSize:16.0f];
    self.placeTextView.m_placeHolder = @"说点什么吧";
    self.placeTextView.m_placeholderColor  = [UIColor colorWithWhite:109/255.0f alpha:1];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitleViewWith:@"写评论"];
    [self bulidLayout];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 50, 44);
    [btn setTitle:@"发布" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(commentClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = item;
    [self.activity startAnimating];
    [self startLocation];
}

-(void)commentClick{
    NSString *addressStr = [CJJHelper trimmingWhitespace:self.addressLabel.text];
    NSString *content = [CJJHelper trimmingWhitespace:self.placeTextView.text];
    if (self.addressLabel.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"正在定位中请稍后" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    if(content.length == 0){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请输入发表内容" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId,@"subjectId":self.subjectId,@"address":addressStr,@"content":content};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpComment];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0)
            {
                [DearLoadingView showTips:dict[@"Message"]];
                [DearUser sharedInstance].isRefresh = YES;
                [self.navigationController popViewControllerAnimated:YES];
                
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];

}
- (void)startLocation{
    UploadRequest *tool = [UploadRequest sharedInstance];
    tool.delegate = self;
    [tool getLocation];
    [_activity startAnimating];
}

- (void)getLocationAddress:(NSString *)address{
    [_activity stopAnimating];
    _activity.hidesWhenStopped = YES;
    self.addressLabel.text = address;
    self.showLabel.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
